#include "H/Function_Init.H"


uint8_t adc_sw(uint16_t adc_v)
{
	uint8_t ret;
	if(204 > adc_v)
		ret = 0;
	else if(613 > adc_v)
		ret = 1;
	else if(1022 > adc_v)
		ret = 2;
	else if(1431 > adc_v)
		ret = 3;
	else if(1840 > adc_v)
		ret = 4;
	else if(2249 > adc_v)
		ret = 5;
	else if(2658 > adc_v)
		ret = 6;
	else if(3067 > adc_v)
		ret = 7;
	else if(3476 > adc_v)
		ret = 8;
	else if(3885 > adc_v)
		ret = 9;
	else
		ret = 9;
	
	return ret;
}


bit adc_read_flag;
uint8_t adc_read_cnt=0;
uint16_t RollX_Value, RollY_Value;
uint8_t roll_x=0, roll_y=0;
void ADC_Read(void)
{
	if(!adc_read_flag)return;
	adc_read_flag = 0;
	
	switch(adc_read_cnt)
	{
		case 0:{
			ADC_Init(AIN11);
			ADCCON |= 0X40; 
		}break;
		
		case 1:{
			RollX_Value = (ADCVH<<4)+(ADCVL>>4);
//			RollX_Value >>= 3;
			roll_x = adc_sw(RollX_Value);
		}break;
		
		case 2:{
			ADC_Init(AIN10);
			ADCCON |= 0X40; 
		}break;
		
		case 3:{
			RollY_Value = (ADCVH<<4)+(ADCVL>>4);
			roll_y = adc_sw(RollY_Value);
		}break;
	}
	
	adc_read_cnt++;
	if(4 <= adc_read_cnt)adc_read_cnt = 0;
}


bit butn_read_flag;
uint8_t xdata butn_filter_cnt;
uint8_t xdata Button_M;
void Button_Read(void)
{
	if(!butn_read_flag)return;
	butn_read_flag = 0;
	
	if(0 == BUTTON_IN)
	{
		butn_filter_cnt++;
		if(20 <= butn_filter_cnt)
		{
			Button_M = 1;
			Claw_Start();
		}
	}else{
		Button_M = 0;
		butn_filter_cnt = 0;
	}
}


